<template>
  <div id="app">
    <el-card shadow="always">
      <el-form :inline="true" size="medium" :model="tbspecSearch" class="demo-form-inline">
        <el-form-item >
          <el-input v-model="tbspecSearch.specname" placeholder="规格名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"  @click="search" icon="el-icon-user">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success"  @click="edittbspec" icon="el-icon-plus">添加规格和规格设置</el-button>
        </el-form-item>
      </el-form>

      <el-table
          :data="tbspecList"
          style="width: 100%">
        <el-table-column
            label="规格ID"
            prop="id"
            align="center"
            width="180">
        </el-table-column>
        <el-table-column
            label="规格名称"
            align="center"
            prop="specName">
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="success"
                @click="handleEdit(scope.$index, scope.row)">修改</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页导航-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[2, 4, 6, 8]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
    <!--对话框-->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <el-form :model="tbspec">
        <el-form-item label="规格ID" :label-width="formLabelWidth">
          <el-input v-model="tbspec.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="规格名称" :label-width="formLabelWidth">
          <el-input v-model="tbspec.specName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="savetbspec">确定并进入新增规格选项</el-button>
      </div>
    </el-dialog>

    </el-dialog>
        <el-dialog :title="title2" :close-on-click-modal="false" :visible.sync="dialogFormVisible2">
          <el-form :model="tbspecop">
            <el-form-item label="规格设置ID" :label-width="formLabelWidth">
              <el-input v-model="tbspecop.id" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="规格设置名称" :label-width="formLabelWidth">
              <el-input v-model="tbspecop.optionName" autocomplete="off"></el-input>
            </el-form-item>
        <el-form-item >
          <el-select v-model="tbspecop.specId" placeholder="所属规格ID">
            <el-option :label="o.specName" :value="o.id" v-for="o in tbspecList2" :key="o.id"></el-option>
          </el-select>
       </el-form-item>
        <el-form-item label="Orders" :label-width="formLabelWidth">
              <el-input v-model="tbspecop.orders" autocomplete="off"></el-input>
            </el-form-item>
        </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible2 = false">取 消</el-button>
            <el-button type="primary" @click="savetbspecop">确 定</el-button>
          </div>
     </el-dialog>

  </div>
</template>

<script>
import tbspecApi from "@/api/TbSpecification"
import tbspecopApi from "@/api/TbSpecificationOption"

export default {
  data() {
    return {
      tbspecList: [],
      tbspecList2: [],
      page:1,
      pageSize:2,
      total:100,
      tbspecSearch: {},
      dialogFormVisible:false,
      dialogFormVisible2:false,
      formLabelWidth:'100px',
      tbspec: {},
      tbspecop: {},
      title: '添加规格',
      title2: '添加规格设置',
    }
  },
  created() {
    this.findspec()
    this.search()
  },

  methods: {

     async findspec(){
         const {data:res} = await tbspecApi.findspec()
         this.tbspecList2 = res
     },

    async search(){
      const {data} = await tbspecApi.search(this.page,this.pageSize,this.tbspecSearch)
      this.tbspecList = data.rows
      this.total = data.total
    },

    edittbspec(){
      this.dialogFormVisible = true
    },

    async savetbspec(){
      const {data:res} = await tbspecApi.save(this.tbspec)
      if (res.code === 200){
        this.$message({
          type: 'success',
          message: res.message
        })

        this.dialogFormVisible = false
        this.dialogFormVisible2 =true
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },

    async savetbspecop(){
      const {data:res} = await tbspecopApi.saveop(this.tbspecop)
      if (res.code === 200){
        this.$message({
          type: 'success',
          message: res.message
        })

        this.dialogFormVisible2 =false
        this.search()

      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },

    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    // 每页大小改变时事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.page = 1;
      this.search()
    },
    // 当前页改变时事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.search()
    }
  }
}
</script>
<style>
.el-card{
  width: 90%;
//margin: 0 auto;
}
#app{
  display: flex;
  justify-content: center;
}
.el-pagination{
  text-align: right;
  margin-top: 15px;
}
</style>
